package com.example.demo.controller;

import com.example.demo.config.WebSocketServer;
import com.example.demo.entity.Chatroom;
import com.example.demo.pojo.PlannerResult;
import com.example.demo.repository.ChatroomRepository;
import com.example.demo.repository.UserRepository;
import com.example.demo.vo.ChatroomVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/*

本controller目前无用

 */
@RestController
@RequestMapping("/chatroom")
public class ChatroomController {
    @Autowired
    ChatroomRepository chatroomRepository;
    @Autowired
    private UserRepository userRepository;

    /*
     @PostMapping("/createchatroom")
    public PlannerResult<String> save(@RequestBody ChatroomVo chatroomVo)
    {
        Chatroom chatroom = new Chatroom();
        chatroom.setContext(chatroomVo.getContext());
        chatroom.setUser_id(chatroomVo.getUser_id());
        chatroom.setActivity_id(chatroomVo.getActivity_id());
        chatroom.setSend_time(chatroomVo.getSend_time());

        Chatroom result=chatroomRepository.save(chatroom);
        if(result.getId() !=null)
        {
            return PlannerResult.success("success");
        }
        else {
            return PlannerResult.error("error");
        }
    }
     */

    //查本活动的所有chatroom，应该没用
    @GetMapping("/selectchatroom")
    public PlannerResult<List<ChatroomVo>> select_chatrom (@RequestParam("activityid") Integer activityid)
    {
        List<Chatroom> chatroomList = chatroomRepository.findAllByActivityId(activityid);
        List<ChatroomVo> result = chatroomList.stream().map(chatroom -> {
            ChatroomVo chatroomVo = new ChatroomVo();
            BeanUtils.copyProperties(chatroom,chatroomVo);
            chatroomVo.setUsername(userRepository.findById(chatroom.getUser_id()).get().getUsername());
            chatroomVo.setAvatar(userRepository.findById(chatroom.getUser_id()).get().getAvatar());
            return chatroomVo;
        }).toList();

        return PlannerResult.success(result);
    }

}

